using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using DL;
/// <summary>
/// Summary description for ProductDAL
/// </summary>
namespace DAL 
{
    
    public class ProductDAL  : BaseDAL
    {
    	#region Private Variables

		#endregion
		
 #region Public Constructors
        public ProductDAL()
        {
            //
            // TODO: Add constructor logic here
            //
            
        }
 #endregion       

	
		
#region Public Methods
        public int Insert(ProductDL objProductDL)
        {
            
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType =  CommandType.StoredProcedure;
            Sqlcomm.CommandText =  "Product_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@ProductCode", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.ProductCode;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Name", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.Name;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Avatar", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.Avatar;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Discription", SqlDbType.NText);
Sqlparam.Value = objProductDL.Discription;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Tag", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.Tag;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Content", SqlDbType.NText);
Sqlparam.Value = objProductDL.Content;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Price", SqlDbType.Float);
Sqlparam.Value = objProductDL.Price;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@PricePromotion", SqlDbType.Float);
Sqlparam.Value = objProductDL.PricePromotion;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@CreateTime", SqlDbType.DateTime);
Sqlparam.Value = objProductDL.CreateTime;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Warranty", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.Warranty;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@StatusId", SqlDbType.Int);
Sqlparam.Value = objProductDL.StatusId;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@IsProductSellMore", SqlDbType.Bit);
Sqlparam.Value = objProductDL.IsProductSellMore;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductHot", SqlDbType.Bit);
Sqlparam.Value = objProductDL.ProductHot;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductNew", SqlDbType.Bit);
Sqlparam.Value = objProductDL.ProductNew;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Order", SqlDbType.Int);
Sqlparam.Value = objProductDL.Order;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@View", SqlDbType.Int);
Sqlparam.Value = objProductDL.View;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductPromotion", SqlDbType.Bit);
Sqlparam.Value = objProductDL.ProductPromotion;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductTypeId", SqlDbType.Int);
Sqlparam.Value = objProductDL.ProductTypeId;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
Sqlparam.Direction = ParameterDirection.ReturnValue;
Sqlcomm.Parameters.Add(Sqlparam);

           
            int result =base.ExecuteNoneQuery(Sqlcomm);
            
            if(!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
				result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(ProductDL objProductDL)
        {
            
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType =  CommandType.StoredProcedure;
            Sqlcomm.CommandText =  "Product_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Id", SqlDbType.Int);
Sqlparam.Value = objProductDL.Id;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductCode", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.ProductCode;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Name", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.Name;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Avatar", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.Avatar;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Discription", SqlDbType.NText);
Sqlparam.Value = objProductDL.Discription;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Tag", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.Tag;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Content", SqlDbType.NText);
Sqlparam.Value = objProductDL.Content;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Price", SqlDbType.Float);
Sqlparam.Value = objProductDL.Price;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@PricePromotion", SqlDbType.Float);
Sqlparam.Value = objProductDL.PricePromotion;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@CreateTime", SqlDbType.DateTime);
Sqlparam.Value = objProductDL.CreateTime;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Warranty", SqlDbType.NVarChar);
Sqlparam.Value = objProductDL.Warranty;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@StatusId", SqlDbType.Int);
Sqlparam.Value = objProductDL.StatusId;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@IsProductSellMore", SqlDbType.Bit);
Sqlparam.Value = objProductDL.IsProductSellMore;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductHot", SqlDbType.Bit);
Sqlparam.Value = objProductDL.ProductHot;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductNew", SqlDbType.Bit);
Sqlparam.Value = objProductDL.ProductNew;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@Order", SqlDbType.Int);
Sqlparam.Value = objProductDL.Order;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@View", SqlDbType.Int);
Sqlparam.Value = objProductDL.View;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductPromotion", SqlDbType.Bit);
Sqlparam.Value = objProductDL.ProductPromotion;
Sqlcomm.Parameters.Add(Sqlparam);

Sqlparam = new SqlParameter("@ProductTypeId", SqlDbType.Int);
Sqlparam.Value = objProductDL.ProductTypeId;
Sqlcomm.Parameters.Add(Sqlparam);


            
            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);
            
            int result=base.ExecuteNoneQuery(Sqlcomm);
            
             if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);
                
            return result;

           
        }

        public int Delete(ProductDL objProductDL)
        {
            
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType =  CommandType.StoredProcedure;
            Sqlcomm.CommandText =  "Product_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@Id", SqlDbType.Int);
Sqlparam.Value = objProductDL.Id;
Sqlcomm.Parameters.Add(Sqlparam);


            
            int result=base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

         public int DeleteAll()
        {
            
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType =  CommandType.StoredProcedure;
            Sqlcomm.CommandText =  "Product_DeleteAll";

            int result=base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public ProductDL Select(ProductDL objProductDL)
        {
            
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType =  CommandType.StoredProcedure;
            Sqlcomm.CommandText =  "Product_GetByPK";
            SqlParameter Sqlparam;
  

            Sqlparam = new SqlParameter("@Id", SqlDbType.Int);
Sqlparam.Value = objProductDL.Id;
Sqlcomm.Parameters.Add(Sqlparam);


            
            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if(!Convert.IsDBNull(dr["Id"]))
objProductDL.Id=Convert.ToInt32(dr["Id"]);
if(!Convert.IsDBNull(dr["ProductCode"]))
objProductDL.ProductCode=Convert.ToString(dr["ProductCode"]);
if(!Convert.IsDBNull(dr["Name"]))
objProductDL.Name=Convert.ToString(dr["Name"]);
if(!Convert.IsDBNull(dr["Avatar"]))
objProductDL.Avatar=Convert.ToString(dr["Avatar"]);
if(!Convert.IsDBNull(dr["Discription"]))
objProductDL.Discription=Convert.ToString(dr["Discription"]);
if(!Convert.IsDBNull(dr["Tag"]))
objProductDL.Tag=Convert.ToString(dr["Tag"]);
if(!Convert.IsDBNull(dr["Content"]))
objProductDL.Content=Convert.ToString(dr["Content"]);
if(!Convert.IsDBNull(dr["Price"]))
objProductDL.Price=Convert.ToDouble(dr["Price"]);
if(!Convert.IsDBNull(dr["PricePromotion"]))
objProductDL.PricePromotion=Convert.ToDouble(dr["PricePromotion"]);
if(!Convert.IsDBNull(dr["CreateTime"]))
objProductDL.CreateTime=Convert.ToDateTime(dr["CreateTime"]);
if(!Convert.IsDBNull(dr["Warranty"]))
objProductDL.Warranty=Convert.ToString(dr["Warranty"]);
if(!Convert.IsDBNull(dr["StatusId"]))
objProductDL.StatusId=Convert.ToInt32(dr["StatusId"]);
if(!Convert.IsDBNull(dr["IsProductSellMore"]))
objProductDL.IsProductSellMore=Convert.ToBoolean(dr["IsProductSellMore"]);
if(!Convert.IsDBNull(dr["ProductHot"]))
objProductDL.ProductHot=Convert.ToBoolean(dr["ProductHot"]);
if(!Convert.IsDBNull(dr["ProductNew"]))
objProductDL.ProductNew=Convert.ToBoolean(dr["ProductNew"]);
if(!Convert.IsDBNull(dr["Order"]))
objProductDL.Order=Convert.ToInt32(dr["Order"]);
if(!Convert.IsDBNull(dr["View"]))
objProductDL.View=Convert.ToInt32(dr["View"]);
if(!Convert.IsDBNull(dr["ProductPromotion"]))
objProductDL.ProductPromotion=Convert.ToBoolean(dr["ProductPromotion"]);
if(!Convert.IsDBNull(dr["ProductTypeId"]))
objProductDL.ProductTypeId=Convert.ToInt32(dr["ProductTypeId"]);

            }
             return objProductDL;
        }

        public ArrayList SelectAll1( )
        {
			
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType =  CommandType.StoredProcedure;
            Sqlcomm.CommandText =  "Product_GetAll";
            
            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrProductDL = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach(DataRow dr in dt.Rows)
{
ProductDL objProductDL= new ProductDL();
if(!Convert.IsDBNull(dr["Id"]))
objProductDL.Id=Convert.ToInt32(dr["Id"]);
if(!Convert.IsDBNull(dr["ProductCode"]))
objProductDL.ProductCode=Convert.ToString(dr["ProductCode"]);
if(!Convert.IsDBNull(dr["Name"]))
objProductDL.Name=Convert.ToString(dr["Name"]);
if(!Convert.IsDBNull(dr["Avatar"]))
objProductDL.Avatar=Convert.ToString(dr["Avatar"]);
if(!Convert.IsDBNull(dr["Discription"]))
objProductDL.Discription=Convert.ToString(dr["Discription"]);
if(!Convert.IsDBNull(dr["Tag"]))
objProductDL.Tag=Convert.ToString(dr["Tag"]);
if(!Convert.IsDBNull(dr["Content"]))
objProductDL.Content=Convert.ToString(dr["Content"]);
if(!Convert.IsDBNull(dr["Price"]))
objProductDL.Price=Convert.ToDouble(dr["Price"]);
if(!Convert.IsDBNull(dr["PricePromotion"]))
objProductDL.PricePromotion=Convert.ToDouble(dr["PricePromotion"]);
if(!Convert.IsDBNull(dr["CreateTime"]))
objProductDL.CreateTime=Convert.ToDateTime(dr["CreateTime"]);
if(!Convert.IsDBNull(dr["Warranty"]))
objProductDL.Warranty=Convert.ToString(dr["Warranty"]);
if(!Convert.IsDBNull(dr["StatusId"]))
objProductDL.StatusId=Convert.ToInt32(dr["StatusId"]);
if(!Convert.IsDBNull(dr["IsProductSellMore"]))
objProductDL.IsProductSellMore=Convert.ToBoolean(dr["IsProductSellMore"]);
if(!Convert.IsDBNull(dr["ProductHot"]))
objProductDL.ProductHot=Convert.ToBoolean(dr["ProductHot"]);
if(!Convert.IsDBNull(dr["ProductNew"]))
objProductDL.ProductNew=Convert.ToBoolean(dr["ProductNew"]);
if(!Convert.IsDBNull(dr["Order"]))
objProductDL.Order=Convert.ToInt32(dr["Order"]);
if(!Convert.IsDBNull(dr["View"]))
objProductDL.View=Convert.ToInt32(dr["View"]);
if(!Convert.IsDBNull(dr["ProductPromotion"]))
objProductDL.ProductPromotion=Convert.ToBoolean(dr["ProductPromotion"]);
if(!Convert.IsDBNull(dr["ProductTypeId"]))
objProductDL.ProductTypeId=Convert.ToInt32(dr["ProductTypeId"]);
arrProductDL.Add(objProductDL);
}
            }
               return arrProductDL;
        }
        
        public DataTable SelectAll( )
        {
			
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType =  CommandType.StoredProcedure;
            Sqlcomm.CommandText =  "Product_GetAll";
            
            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
          
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                
            }
               return dt;
        }

     
#endregion          
    
    }

}
